import json
import requests
from xml.etree import ElementTree
from datetime import datetime
import hashlib


def send_message_by_request(phone, msg):
    account = "N4411918"  # 修改为您的API账号
    password = "340fepcvB"  # 修改为您的API密码
    # domain_name = "smssh1.253.com"  # 创蓝发送短信域名
    url = "https://smssh1.253.com/msg/send/json"  # 创蓝发送短信接口URL
    # port = 80
    # phone = "17513526520"  # 接收短信手机号
    phone = phone  # 接收短信手机号
    # msg = "【253云通讯】您的验证码是：222222"
    msg = msg
    param = {'account': account,
             'password': password,
             'msg': msg,
             'phone': phone,
             'report': 'true'}
    headers = {"Content-type": "application/json"}
    res = requests.post(url=url, headers=headers, json=param)
    # print("------------------------")
    # print(res.status_code)
    print(res.content.decode('utf8'))
    hh = json.loads(res.content.decode('utf8'))
    if hh["code"] == "0":
        return True
    else:
        return False


def send_sms_message(mobile, message):
    try:
        username = '17303810352'
        password = '88DBDA62DBF4F64EE9C8DAE693B970A9'
        url = 'http://api.sms1086.com/Api/verifycode.aspx?'
        # gwid = 'c06cc5ac'
        params = {
            'username': username,
            'password': password,
            'message': message,
            'mobile': mobile,
        }
        headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
        res = requests.get(url=url, headers=headers, params=params)

        data = ElementTree.XML(res.content.decode('utf8'))
        content = data.find('code').text
        # content = json.loads(res.content.decode('utf8'))
        if content == "0":
            return True
        else:
            print(res.content.decode('utf8'))
            return False
    except Exception as e:
        print(e)
        return False


def send_sms_captcha(mobile, message):
    try:
        username = '17303810352'.encode('gb2312')
        password = 'shinuo2018'
        url = 'http://api.sms1086.com/Api/verifycode.aspx?'
        now = (datetime.now()).strftime('%Y-%m-%d %H:%M:%S')
        pwd_str = password + now
        pwd_bytes = bytes(pwd_str, encoding="utf8")
        pwd = hashlib.md5(pwd_bytes).hexdigest()
        # print(pwd)
        # gwid = 'c06cc5ac'
        params = {
            'username': username,
            'password': pwd,
            'content': message.encode('gb2312'),
            'mobiles': mobile,
            'timestamp': now,
        }
        headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
        res = requests.get(url=url, headers=headers, params=params)
        # print(res.content.decode('utf8'))
        data = res.content.decode('utf8')
        content = data.split('&')
        # content = json.loads(res.content.decode('utf8'))
        if content[0] == "result=0":
            return True
        else:
            print(res.content.decode('utf8'))
            print(res.content)
            return False
    except Exception as e:
        print(e)
        return False


if __name__ == '__main__':
    phone = ""                                 # 接收短信手机号
    msg = "【】您的验证码是{}，请不要告诉他人。".format('1234')        # 发送短信内容
    # send_sms(msg, phone)
    send_sms_captcha(phone, msg)
